<?php

require("config.php");
require("fonctions.php");

$action = $_GET["action"];
$login = $_POST["login"];
$password = $_POST["password"];
$profil = $_POST["profil"];
$nom = $_POST["nom"];
$prenom = $_POST["prenom"];
$instit = $_POST["instit"];
$email = $_POST["email"];
$fonction = $_POST["fonction"];
$id = $_POST["id"];
$tel = $_POST["tel"];

if ($action == "login") {
    $login = $_POST["login"];
    $password = $_POST["password"];
    $lang = $_POST["lang"];


    $sql = "select * from users where login='$login' and password='$password'";
    if (mysql_num_rows($res = mysql_query($sql))) {
        $row = mysql_fetch_object($res);
        // d&eacute;but de la sessions
        session_start();
        // enregistrement d'une variable de session, ici le login de l'utilisateur
        $_SESSION["login"] = $login;
        $_SESSION["profil"] = $row->profil;
        $_SESSION["id_user"] = $row->id;
        $_SESSION["lang"] = $lang;

        histo_connection($row->id, $row->profil);
        if ($row->profil == "ADMIN") {
            header("Location:ad_acc.php?cur=1");
        } else {
            header("Location:type_prog.php");
        }
    } else {
        header("Location:index.php?error=13");
    }
}

if ($action == "type_prog") {

    $type_prog = $_POST["type_prog"];
    $type_prog_theme = $_POST["type_prog_theme"];
    if (($type_prog > 0) && ($type_prog_theme > 0)) {
        // debut de la sessions
        session_start();
        $_SESSION["type_prog"] = $type_prog;
        //if ($_SESSION["profil"]== "USER") {
        if ($type_prog == 1) {
            header("Location:form_1.php?cur=2&type_prog=1");
        }
        if ($type_prog == 2) {
            header("Location:form_2.php?cur=2&type_prog=2");
        }
        if ($type_prog == 3) {
            header("Location:form_3.php?cur=2&type_prog=3");
        }
        if ($type_prog == 4) {
            header("Location:form_4.php?cur=2&type_prog=4");
        }
        if ($type_prog == 5) {
            header("Location:form_5.php?cur=2&type_prog=5");
        }
        if ($type_prog == 6) {
            header("Location:form_6.php?cur=2&type_prog=6");
        }
        if ($type_prog == 7) {
            header("Location:form_7.php?cur=2&type_prog=7");
        }
        if ($type_prog == 8) {
            header("Location:form_8.php?cur=2&type_prog=8");
        }
        if ($type_prog == 9) {
            header("Location:form_9.php?cur=2&type_prog=9");
        }
        if ($type_prog == 10) {
            header("Location:form_10.php?cur=2&type_prog=10");
        }
        if ($type_prog == 11) {
            header("Location:form_11.php?cur=2&type_prog=11");
        }
        if ($type_prog == 12) {
            header("Location:form_12.php?cur=2&type_prog=12");
        }
        if ($type_prog == 13) {
            header("Location:form_13.php?cur=2&type_prog=13");
        }
        if ($type_prog == 14) {
            header("Location:form_14.php?cur=2&type_prog=14");
        }
        if ($type_prog == 15) {
            header("Location:form_15.php?cur=2&type_prog=15");
        }
        if ($type_prog == 16) {
            header("Location:form_16.php?cur=2&type_prog=16");
        }
    } else {
        header("Location:type_prog.php?error=14");
    }
}

if ($action == "add_user") {

    $sql = "insert into users(login,password,profil,nom,prenom,fonction,instit,email,tel) values('$login','$password','$profil','$nom','$prenom','$fonction','$instit','$email','$tel')";
    mysql_query($sql);
    header("Location:ad_users.php?cur=2&error=1");
}

if ($action == "mod_user") {
    $sql = "update users set login='$login',password='$password',profil='$profil',nom='$nom',prenom='$prenom',instit='$instit',email='$email',fonction='$fonction',tel='$tel' where id='$id'";
    mysql_query($sql);
    header("Location:ad_users.php?cur=2&error=5");
}

if ($action == "mod_user_profil") {
    $sql = "update users set login='$login',password='$password',profil='$profil',nom='$nom',prenom='$prenom',instit='$instit',email='$email',fonction='$fonction',tel='$tel' where id='$id'";
    mysql_query($sql);
    header("Location:profil.php?cur=1&error=5");
}

if ($action == "sup_user") {
    $id = $_GET["id"];
    $sql = "delete from users where id='$id'";
    mysql_query($sql);
    header("Location:ad_users.php?cur=2&error=7");
}

if ($action == "sup_rappel") {
    $id = $_GET["id"];
    $sql = "delete from campagnes where id='$id'";
    mysql_query($sql);
    header("Location:ad_rappels.php?cur=5&error=7");
}

if ($action == "add_rappel") {
    $num_camp = $_POST["num_camp"];
    $nom = $_POST["nom"];

    $descr = addslashes($_POST["descr"]);
    $date_deb = dateFR2US($_POST["date_deb"]);
    $date_fin = dateFR2US($_POST["date_fin"]);
    $sql = "insert into campagnes (num_camp,nom,descr,date_deb,date_fin) values('$num_camp','$nom','$descr','$date_deb','$date_fin')";
    mysql_query($sql);
    header("Location:ad_rappels.php?cur=5&error=7");
}

if ($action == "status") {
    $id = $_GET["id"];
    $status = getUserStatut($id);
    if ($status == "ON") {
        $val = "OFF";
    } else {
        $val = "ON";
    }
    $sql = "update users set statut='$val' where id='$id'";
    mysql_query($sql);
    header("Location:ad_users.php?cur=2&error=8");
}
if ($action == "oublie") {
    $email = $_POST["email"];

    $sql3 = "select * from users where  email='$email'";
    if (mysql_num_rows($res3 = mysql_query($sql3)) > 0) {

        $r3 = mysql_fetch_object($res3);

        $subject = "Rappel Identification";
        $message = "Bonjour votre identifiant est : $r3->login , votre mot de passe : $r3->password";
        $headers = "";
        $headers = 'From: admin@lvdc.com' . "\r\n" .
                'Reply-To: admin@lvdc.com' . "\r\n" .
                'X-Mailer: PHP/' . phpversion();
        //echo $message;
        @@mail($email, $subject, $message, $headers);
        header("Location:index.php?cur=16&error=10");
    } else {
        header("Location:index.php?cur=16&error=11");
    }
}

$id_camp = $_POST[""];
$nom = $_POST["nom_prod"];
$prenom = $_POST["prenom_prod"];
$sexe = $_POST["sexe"];
$nom_media = $_POST["media"];
$pays = $_POST["pays"];
$localite = $_POST["localite"];
$titre_prod = $_POST["titre_prod"];
$num_camp = $_POST["num_camp"];
$theme = $_POST["type_theme"];
$date_dif = $_POST["date_deb"];
$date_redif = $_POST["date_fin"];
$nbre_redif = $_POST["num_redif"];
$duree_prod = $_POST["duree_prod"];
$langue_prod = $_POST["langue_prod"];
$resume_prod = $_POST["resume_cont"];

// narrateur 1
$nom_nar = $_POST["nom_nar"];
$prenom_nar = $_POST["prenom_nar"];
$sexe_nar = $_POST["sexe_nar"];
$profession_nar = $_POST["profession_nar"];
$profession_autre_nar = $_POST["profession_autre_nar"];
$age_nar = $_POST["age_nar"];
$type_marginalise_nar = $_POST["type_marginalise_nar"];
$autres_nar = $_POST["autres_nar"];
$contribution_nar = $_POST["contribution_nar"];

// narrateur 2

$nom_nar2 = $_POST["nom_nar2"];
$prenom_nar2 = $_POST["prenom_nar2"];
$sexe_nar2 = $_POST["sexe_nar2"];
$profession_nar2 = $_POST["profession_nar2"];
$profession_autre_nar2 = $_POST["profession_autre_nar2"];
$age_nar2 = $_POST["age_nar2"];
$type_marginalise_nar2 = $_POST["type_marginalise_nar2"];
$autres_nar2 = $_POST["autres_nar2"];
$contribution_nar2 = $_POST["contribution_nar2"];

$contraintes = $_POST["contraintes"];
$type_problem = $_POST["type_problem"];
$autres_prod = $_POST["autres_prod"];

$nom_exp = $_POST["nom_exp"];
$prenom_exp = $_POST["prenom_exp"];
$sexe_exp = $_POST["sexe_exp"];
$contribution_exp = $_POST["contribution_exp"];
$profession_exp = $_POST["profession_exp"];
$age_exp = $_POST["age_exp"];

$reactions_aud = $_POST["reactions_aud"];
$statut_aud = $_POST["statut_aud"];
$langue_aud = $_POST["langue_aud"];

$id_fiche = round(microtime() * 10000);
$id_user = $_POST["id_user"];
$id_form = $_POST["id_form"];


// fiche 2
$statut_reprise = $_POST["statut_reprise"];
$nb_reprises = $_POST["nb_reprises"];
$lien1 = $_POST["lien1"];
$lien2 = $_POST["lien2"];
$lien3 = $_POST["lien2"];
$nbre_vus = $_POST["nbre_vus"];
$pays1 = $_POST["pays1"];
$pays2 = $_POST["pays2"];
$pays3 = $_POST["pays3"];
$resume = $_POST["resume"];

// fiche 3

$profession= $_POST["profession"];
$age= $_POST["age"];
$contribution= $_POST["contribution"];

insertUsersFiches($id_user, $id_fiche, $id_form);

if ($action == "add_1") {

    insertInfoGen($id_fiche, $id_camp, $nom, $prenom, $sexe, $nom_media, $pays, $localite);

    insertInfoProd($id_fiche, $id_camp, $titre_prod, $num_camp, $theme, $date_dif, $date_redif, $nbre_redif, $duree_prod, $langue_prod, $resume_prod);

    insertInfoNarrateur($id_fiche, $id_camp, 1, $nom_nar, $prenom_nar, $profession_nar, $profession_autre_nar, $age_nar, $sexe_nar, $type_marginalise_nar, $autres_nar, $contribution_nar);
    insertInfoNarrateur($id_fiche, $id_camp, 2, $nom_nar2, $prenom_nar2, $profession_nar2, $profession_autre_nar2, $age_nar2, $sexe_nar2, $type_marginalise_nar2, $autres_nar2, $contribution_nar2);

    insertAvisExpert($id_fiche, $id_camp, $nom_exp, $prenom_exp, $profession_exp, $age_exp, $sexe_exp, $contribution_exp);

    insertAvisProducteur($id_fiche, $id_camp, $contraintes, $type_problem, $autres_prod);

    insertAuditeurs($id_fiche, $id_camp, $reactions_aud, $statut_aud, $langue_aud);

    header("Location:form_1.php?cur=2&error=6");
}
if ($action == "add_2") {

    insertInfoGen($id_fiche, $id_camp, $nom, $prenom, $sexe, $nom_media, $pays, $localite);

    insertInfoProd($id_fiche, $id_camp, $titre_prod, $num_camp, $theme, $date_dif, $date_redif, $nbre_redif, $duree_prod, $langue_prod, $resume_prod);

    insertInfoNarrateur($id_fiche, $id_camp, 1, $nom_nar, $prenom_nar, $profession_nar, $profession_autre_nar, $age_nar, $sexe_nar, $type_marginalise_nar, $autres_nar, $contribution_nar);

    insertInfoNarrateur($id_fiche, $id_camp, 2, $nom_nar2, $prenom_nar2, $profession_nar2, $profession_autre_nar2, $age_nar2, $sexe_nar2, $type_marginalise_nar2, $autres_nar2, $contribution_nar2);

    insertAvisExpert($id_fiche, $id_camp, $nom_exp, $prenom_exp, $profession_exp, $age_exp, $sexe_exp, $contribution_exp);

    insertAvisDiffusion($id_fiche, $id_camp, $statut_reprise, $nb_reprises, $lien1, $lien2, $lien3, $nbre_vus, $pays1, $pays2, $pays3, $resume);

    insertAvisProducteur($id_fiche, $id_camp, $contraintes, $type_problem, $autres_prod);

    header("Location:form_2.php?cur=2&error=6");
}

if ($action == "add_3") {
    
     insertInfoGen($id_fiche, $id_camp, $nom, $prenom, $sexe, $nom_media, $pays, $localite);

    insertInfoProd($id_fiche, $id_camp, $titre_prod, $num_camp, $theme, $date_dif, $date_redif, $nbre_redif, $duree_prod, $langue_prod, $resume_prod);
    
    insertInfosRessources($id_fiche,$id_camp,$nom,$prenom,$profession,$age,$sexe,$contribution);
    
    
    
    
    header("Location:form_3.php?cur=2&error=6");
}

if ($action == "add_4") {
    header("Location:form_4.php?cur=2&error=6");
}

if ($action == "add_5") {
    header("Location:form_5.php?cur=2&error=6");
}

if ($action == "add_6") {
    header("Location:form_6.php?cur=2&error=6");
}

if ($action == "add_7") {
    header("Location:form_7.php?cur=2&error=6");
}

if ($action == "add_8") {
    header("Location:form_8.php?cur=2&error=6");
}

if ($action == "add_9") {
    header("Location:form_9.php?cur=2&error=6");
}

if ($action == "add_10") {
    header("Location:form_10.php?cur=2&error=6");
}

if ($action == "add_11") {
    header("Location:form_11.php?cur=2&error=6");
}

if ($action == "add_12") {
    header("Location:form_12.php?cur=2&error=6");
}

if ($action == "add_13") {
    header("Location:form_13.php?cur=2&error=6");
}

if ($action == "add_14") {
    header("Location:form_14.php?cur=2&error=6");
}

if ($action == "add_15") {
    header("Location:form_15.php?cur=2&error=6");
}

if ($action == "add_16") {
    header("Location:form_16.php?cur=2&error=6");
}
?>